package com.first.config.redis.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

import com.first.config.redis.JredisCache;

@Configuration
@ConditionalOnClass(JredisCache.class)
@EnableCaching //启用缓存，这个注解很重要；
public class CacheConfig  extends CachingConfigurerSupport{
//	@Resource(name="JedisCacheConnectionFactory")
//	  @Resource
//	 private JedisConnectionFactory jedisConnectionFactory;
	@Resource(name="redisTemplateCacheForCache")
	 private RedisTemplate<Object, Object> redisTemplateCacheForCache;
	 
	 @Bean
	    public RedisCacheManager cacheManager() {
//		    System.out.println("CacheConfig---"+jedisConnectionFactory.getDatabase());
	        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplateCacheForCache);
//	       设置 key-value 超时时间  对所有缓存统一有效 
//	        redisCacheManager.setDefaultExpiration(10);  
//	         每个缓存的存活时间，不定义这缓存有就有效
	        Map<String, Long> map = new HashMap<>();
	        map.put("signonCache", 10L);
	        redisCacheManager.setExpires(map);
	        return redisCacheManager;
	    }

//	    @Bean
//	    public RedisTemplate<Object, Object> redisTemplate() {
//	        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
////	        jedisConnectionFactory.getConnection().select(2);//  选择二号数据库
//	        jedisConnectionFactory.setDatabase(3);
//	        redisTemplate.setConnectionFactory(jedisConnectionFactory);
//	        // 开启事务支持
//	        redisTemplate.setEnableTransactionSupport(true);
//	        // 使用String格式序列化缓存键 key
//	        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//	        redisTemplate.setKeySerializer(stringRedisSerializer);
//	        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//	        
//	        // value
//	        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
//	        ObjectMapper om = new ObjectMapper();
//	        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//	        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//	        jackson2JsonRedisSerializer.setObjectMapper(om);
//	        
//	        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//	        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
////	        template.setDefaultSerializer(jackson2JsonRedisSerializer);
//	        redisTemplate.afterPropertiesSet();
//	        return redisTemplate;
//	    }
	    /**
	     * 缓存自定义可以生产策略，类名+方法名+参数
	     * @return
	     */
	    @Bean
	    public KeyGenerator myKeyGenerator(){  
	    	  return new KeyGenerator() {  
	              @Override  
	              public Object generate(Object target, Method method, Object... params) {  
	                  StringBuilder sb = new StringBuilder();  
	                  sb.append(target.getClass().getName());  
	                  sb.append(method.getName());  
	                  for (Object obj : params) {  
	                      sb.append(obj.toString());  
	                  }  
	                  return sb.toString();  
	              }  
	          };  
	    }
	    
}
